package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.view.LodingFragmentDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RequestCodeActivity extends BaseActivity implements OnClickListener {

	private String code="";
	private String phone="";
	private Button mTime;
	private EditText mCode;
	private TimeCount timeCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_request_code_layout);
		phone = getIntent().getStringExtra("phone");
		code = getIntent().getStringExtra("code");
		initView();
		timeCount = new TimeCount(120000, 1000);
		timeCount.start();
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		titleTextV.setText("验证码");
		
		mCode = (EditText) findViewById(R.id.code);
		
		mTime = (Button) findViewById(R.id.time);
		
		findViewById(R.id.code_btn).setOnClickListener(this);
		goBack.setOnClickListener(this);
		mTime.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
			
		case R.id.code_btn:
			if(mCode.getText().toString()!=null && !"".equals(mCode.getText().toString())){
				if(code.equals(mCode.getText().toString())){
					Intent intent = new Intent(RequestCodeActivity.this, RegisterFinishActivity.class);
					intent.putExtra("phone",phone);
					intent.putExtra("code", code);
					startActivity(intent);
				}else{
					Toast.makeText(RequestCodeActivity.this, "验证码错误", 0).show();
				}
			}else{
				Toast.makeText(RequestCodeActivity.this, "请输入验证码", 0).show();
			}
			break;
			
		case R.id.time:
			requestCode(phone);
			break;

		default:
			break;
		}
	}
	
	
	/**
	 * @author Administrator 自定义一个倒计时的内部类；
	 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		// 当时间到的时候；
		public void onFinish() {
			// TODO Auto-generated method stub
			mTime.setText("重新获取");
			mTime.setClickable(true);
			mTime.setBackgroundResource(R.drawable.bu_time_red_bg);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			mTime.setClickable(false);
			mTime.setText(millisUntilFinished / 1000 + "秒");
		}
	}
	
	private void requestCode(String tel) {
		 LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(), getResources());
		HttpRequestUtils.doHttpRegisterCode(tel, new AsyncHttpResponseHandler() {
          
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
			
				
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(!jsonO.optBoolean("status")){//{"data":{"code":872890},"errMessage":"","status":true}
						Toast.makeText(RequestCodeActivity.this, jsonO.optString("errMessage"), 0).show();
					}else{
						JSONObject optJSONObject = jsonO.optJSONObject("data");
						
						code = optJSONObject.optString("code");
						timeCount.start();
						mTime.setClickable(false);
						mTime.setBackgroundResource(R.drawable.bu_time_bg);
						
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				LodingFragmentDialog.dismiss(getSupportFragmentManager());
			}
		});
	
}
	
}
